package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import pojos.common.DocHelper;
import pojos.common.SubjectInfo;
import pojos.parents.ParentsManagerInfo;
import pojos.student.OrderSubjectManager;
import pojos.student.StudentInfo;
import pojos.subject.UserRelationshipManager;
import pojos.teacher.TeacherInfo;
import service.CommonUtilsService;
import service.ParentsInfoService;
import service.StudentInfoService;
import service.TeacherInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理员页面控制器
 */
@Controller
@RequestMapping(value = {"/admin"})
public class AdminPageController {

    @Resource(name = "teacherInfoService")
    private TeacherInfoService teacherInfoService;

    @Resource(name = "studentInfoService")
    private StudentInfoService studentInfoService;

    @Resource(name = "parentsInfoService")
    private ParentsInfoService parentsInfoService;

    @Resource(name = "commonUtilsService")
    private CommonUtilsService commonUtilsService;


    /**
     * 控制台主界面
     */
    @GetMapping("/console")
    public ModelAndView adminLogin(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/jsp/admin/Console.jsp");
        modelAndView.addObject("AdminInfo",request.getSession().getAttribute("AdminInfo"));
        return modelAndView;
    }

    /**
     * 框架主页
     */
    @GetMapping("/frame/index")
    public ModelAndView getFrameIndex(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/jsp/admin/Frame-Index.jsp");
        return modelAndView;
    }

    /**
     * 框架管理员信息页
     */
    @GetMapping("/frame/admin-info")
    public ModelAndView getAdminInfo(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("AdminInfo",request.getSession().getAttribute("AdminInfo"));
        modelAndView.setViewName("/jsp/admin/Frame-AdminInfo.jsp");
        return modelAndView;
    }

    /**
     * 框架管理员修改登录信息页
     */
    @GetMapping("/frame/update-login")
    public ModelAndView adminUpdateLoginInfo(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("AdminInfo",request.getSession().getAttribute("AdminInfo"));
        modelAndView.setViewName("/jsp/admin/Frame-UpdateLoginInfo.jsp");
        return modelAndView;
    }

    /**
     * 框架管理员修改基本信息页
     */
    @GetMapping("/frame/update-base")
    public ModelAndView adminUpdateBaseInfo(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("AdminInfo",request.getSession().getAttribute("AdminInfo"));
        modelAndView.setViewName("/jsp/admin/Frame-UpdateBaseInfo.jsp");
        return modelAndView;
    }

    /**
     * 框架教师管理界面
     */
    @GetMapping("/frame/teacherManager")
    public ModelAndView teacherManagerPage(){
        List<TeacherInfo> teacherInfoList = teacherInfoService.queryAllTeachers();

        ModelAndView modelAndView = new ModelAndView();
        if(teacherInfoList != null){
            modelAndView.addObject("TeacherInfoList",teacherInfoList);
        }
        modelAndView.setViewName("/jsp/admin/Frame-TeacherManager.jsp");

        return modelAndView;
    }

    /**
     * 框架添加教师界面
     */
    @GetMapping("/frame/addTeacher")
    public String addTeacherPage(){
        return "forward:/pages/teacher/Console-Frame-AddTeacher.html";
    }

    /**
     * 框架学生管理页
     */
    @GetMapping("/frame/studentManager")
    public ModelAndView studentManagerPage(){
        List<StudentInfo> studentInfoList = studentInfoService.queryAllStudentBaseInfo();

        ModelAndView modelAndView = new ModelAndView();
        if(studentInfoList != null){
            modelAndView.addObject("StudentInfoList",studentInfoList);
        }
        modelAndView.setViewName("/jsp/admin/Frame-StudentManager.jsp");
        return modelAndView;
    }

    /**
     * 框架添加学生界面
     */
    @GetMapping("/frame/addStudent")
    public String addStudentPage(){
        return "forward:/pages/student/Console-Frame-AddStudent.html";
    }

    /**
     * 框架家长管理页
     */
    @GetMapping("/frame/parentsManager")
    public ModelAndView parentsManagerPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/jsp/admin/Frame-ParentsManager.jsp");
        List<ParentsManagerInfo> parentsManagerInfoList = parentsInfoService.queryAllParentsBaseInfo();
        if(parentsManagerInfoList != null){
            modelAndView.addObject("ParentsManagerInfo",parentsManagerInfoList);
        }
        return modelAndView;
    }

    /**
     * 框架添加家长页
     */
    @GetMapping("/frame/addParents")
    public String addParentsPage(){
        return "forward:/pages/parents/Console-Frame-AddParents.html";
    }

    /**
     * 框架科目管理页
     */
    @GetMapping("/frame/subjectManager")
    public ModelAndView subjectManagerPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/jsp/admin/Frame-SubjectManager.jsp");

        List<SubjectInfo> subjectInfoList = commonUtilsService.queryAllSubjectInfo();
        if(subjectInfoList != null){
            modelAndView.addObject("AllSubjectInfo",subjectInfoList);
        }

        return modelAndView;
    }

    /**
     * 框架学生订购课程管理页
     */
    @GetMapping("/frame/studentPay")
    public ModelAndView studentOrderSubjectManagerPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/jsp/admin/Frame-OrderSubjectManager.jsp");

        List<OrderSubjectManager> orderSubjectManagerList = studentInfoService.queryAllStudentOrderSubjectInfo();
        if(orderSubjectManagerList != null){
            modelAndView.addObject("OrderSubjectList",orderSubjectManagerList);
        }
        return modelAndView;
    }

    /**
     * 框架学生课程添加页
     */
    @GetMapping("/frame/addStudentOrder")
    public String addStudentCourse(){
        return "forward:/pages/student/Console-Frame-AddStudentOrderSubject.html";
    }

    /**
     * 查询所有的用户关系
     */
    @GetMapping("/frame/userRelationship")
    public ModelAndView queryAllRelationship(){
        ModelAndView modelAndView = new ModelAndView();
        List<UserRelationshipManager> userRelationshipManagerList = commonUtilsService.queryAllRelation();
        if(userRelationshipManagerList != null){
            modelAndView.addObject("RelationshipList",userRelationshipManagerList);
        }
        modelAndView.setViewName("/jsp/admin/Frame-UserRelationship.jsp");

        return modelAndView;
    }

    /**
     * 查询特定的用户关系
     */
    @GetMapping("/frame/managerRelationship")
    public ModelAndView queryRelationshipById(int id){
        ModelAndView modelAndView = new ModelAndView();

        UserRelationshipManager relationshipManager = commonUtilsService.queryUserRelationship(id);
        if(relationshipManager != null){
            modelAndView.addObject("RelationshipManager",relationshipManager);
        }

        modelAndView.setViewName("/jsp/admin/Frame-RelationshipManager.jsp");

        return modelAndView;
    }

    /**
     * 框架文档管理页
     */
    @GetMapping("/frame/documentManager")
    public String docManagerPage(){
        return "forward:/pages/admin/Frame-DocManager.html";
    }

    /**
     * 框架添加文档页
     */
    @GetMapping("/frame/addDoc")
    public String addDocPage(){
        return "forward:/pages/admin/Frame-AddDocument.html";
    }

    /**
     * 框架文档修改页
     */
    @GetMapping("/frame/update-doc-page")
    public ModelAndView updateDocInfo(int docId){
        ModelAndView modelAndView = new ModelAndView();

        DocHelper docHelper = commonUtilsService.queryDocInfoById(docId);
        if(docHelper != null){
            modelAndView.addObject("Doc",docHelper);
        }

        modelAndView.setViewName("/jsp/admin/Frame-DocUpdate.jsp");

        return modelAndView;
    }
}
